3 * Special handling for file description pages.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Class for viewing MediaWiki file description pages
28 class ImagePage
extends Article
{
40 var $mExtraDescription = false;
44 * @return WikiFilePage
46 protected function newPage( Title
$title ) {
47 // Overload mPage with a file-specific page
48 return new WikiFilePage( $title );
52 * Constructor from a page id
53 * @param $id Int article ID to load
54 * @return ImagePage|null
56 public static function newFromID( $id ) {
57 $t = Title
::newFromID( $id );
58 # @todo FIXME: Doesn't inherit right
59 return $t == null ?
null : new self( $t );
60 # return $t == null ? null : new static( $t ); // PHP 5.3
67 public function setFile( $file ) {
68 $this->mPage
->setFile( $file );
69 $this->displayImg
= $file;
70 $this->fileLoaded
= true;
73 protected function loadFile() {
74 if ( $this->fileLoaded
) {
77 $this->fileLoaded
= true;
79 $this->displayImg
= $img = false;
80 wfRunHooks( 'ImagePageFindFile', array( $this, &$img, &$this->displayImg
) );
81 if ( !$img ) { // not set by hook?
82 $img = wfFindFile( $this->getTitle() );
84 $img = wfLocalFile( $this->getTitle() );
87 $this->mPage
->setFile( $img );
88 if ( !$this->displayImg
) { // not set by hook?
89 $this->displayImg
= $img;
91 $this->repo
= $img->getRepo();
95 * Handler for action=render
96 * Include body text only; none of the image extras
98 public function render() {
99 $this->getContext()->setArticleBodyOnly( true );
103 public function view() {
106 $out = $this->getContext()->getOutput();
107 $request = $this->getContext()->getRequest();
108 $diff = $request->getVal( 'diff' );
109 $diffOnly = $request->getBool( 'diffonly', $this->getContext()->getUser()->getOption( 'diffonly' ) );
111 if ( $this->getTitle()->getNamespace() != NS_FILE ||
( isset( $diff ) && $diffOnly ) ) {
118 if ( $this->getTitle()->getNamespace() == NS_FILE
&& $this->mPage
->getFile()->getRedirected() ) {
119 if ( $this->getTitle()->getDBkey() == $this->mPage
->getFile()->getName() ||
isset( $diff ) ) {
120 // mTitle is the same as the redirect target so ask Article
121 // to perform the redirect for us.
122 $request->setVal( 'diffonly', 'true' );
126 // mTitle is not the same as the redirect target so it is
127 // probably the redirect page itself. Fake the redirect symbol
128 $out->setPageTitle( $this->getTitle()->getPrefixedText() );
129 $out->addHTML( $this->viewRedirect( Title
::makeTitle( NS_FILE
, $this->mPage
->getFile()->getName() ),
130 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
131 $this->mPage
->doViewUpdates( $this->getContext()->getUser() );
136 if ( $wgShowEXIF && $this->displayImg
->exists() ) {
137 // @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
138 $formattedMetadata = $this->displayImg
->formatMetadata();
139 $showmeta = $formattedMetadata !== false;
144 if ( !$diff && $this->displayImg
->exists() ) {
145 $out->addHTML( $this->showTOC( $showmeta ) );
149 $this->openShowImage();
152 # No need to display noarticletext, we use our own message, output in openShowImage()
153 if ( $this->mPage
->getID() ) {
154 # NS_FILE is in the user language, but this section (the actual wikitext)
155 # should be in page content language
156 $pageLang = $this->getTitle()->getPageLanguage();
157 $out->addHTML( Xml
::openElement( 'div', array( 'id' => 'mw-imagepage-content',
158 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
159 'class' => 'mw-content-'.$pageLang->getDir() ) ) );
161 $out->addHTML( Xml
::closeElement( 'div' ) );
163 # Just need to set the right headers
164 $out->setArticleFlag( true );
165 $out->setPageTitle( $this->getTitle()->getPrefixedText() );
166 $this->mPage
->doViewUpdates( $this->getContext()->getUser() );
169 # Show shared description, if needed
170 if ( $this->mExtraDescription
) {
171 $fol = wfMessage( 'shareddescriptionfollows' );
172 if ( !$fol->isDisabled() ) {
173 $out->addWikiText( $fol->plain() );
175 $out->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
178 $this->closeShowImage();
179 $this->imageHistory();
180 // TODO: Cleanup the following
182 $out->addHTML( Xml
::element( 'h2',
183 array( 'id' => 'filelinks' ),
184 wfMsg( 'imagelinks' ) ) . "\n" );
186 # @todo FIXME: For some freaky reason, we can't redirect to foreign images.
187 # Yet we return metadata about the target. Definitely an issue in the FileRepo
190 # Allow extensions to add something after the image links
192 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
194 $out->addHTML( $html );
198 $out->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
199 $out->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
200 $out->addModules( array( 'mediawiki.action.view.metadata' ) );
203 // Add remote Filepage.css
204 if( !$this->repo
->isLocal() ) {
205 $css = $this->repo
->getDescriptionStylesheetUrl();
207 $out->addStyle( $css );
210 // always show the local local Filepage.css, bug 29277
211 $out->addModuleStyles( 'filepage' );
217 public function getDisplayedFile() {
219 return $this->displayImg
;
225 * @param $metadata Boolean: whether or not to show the metadata link
228 protected function showTOC( $metadata ) {
230 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
231 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
232 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
235 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
238 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
240 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
244 * Make a table with metadata to be shown in the output page.
246 * @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
248 * @param $metadata Array: the array containing the EXIF data
249 * @return String The metadata table. This is treated as Wikitext (!)
251 protected function makeMetadataTable( $metadata ) {
252 $r = "<div class=\"mw-imagepage-section-metadata\">";
253 $r .= wfMsgNoTrans( 'metadata-help' );
254 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
255 foreach ( $metadata as $type => $stuff ) {
256 foreach ( $stuff as $v ) {
257 # @todo FIXME: Why is this using escapeId for a class?!
258 $class = Sanitizer
::escapeId( $v['id'] );
259 if ( $type == 'collapsed' ) {
260 $class .= ' collapsable';
262 $r .= "<tr class=\"$class\">\n";
263 $r .= "<th>{$v['name']}</th>\n";
264 $r .= "<td>{$v['value']}</td>\n</tr>";
267 $r .= "</table>\n</div>\n";
272 * Overloading Article's getContent method.
274 * Omit noarticletext if sharedupload; text will be fetched from the
275 * shared upload server if possible.
278 public function getContent() {
280 if ( $this->mPage
->getFile() && !$this->mPage
->getFile()->isLocal() && 0 == $this->getID() ) {
283 return parent
::getContent();
286 protected function openShowImage() {
287 global $wgImageLimits, $wgEnableUploads, $wgSend404Code;
290 $out = $this->getContext()->getOutput();
291 $user = $this->getContext()->getUser();
292 $lang = $this->getContext()->getLanguage();
293 $dirmark = $lang->getDirMarkEntity();
294 $request = $this->getContext()->getRequest();
296 $sizeSel = intval( $user->getOption( 'imagesize' ) );
297 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
298 $sizeSel = User
::getDefaultOption( 'imagesize' );
300 // The user offset might still be incorrect, specially if
301 // $wgImageLimits got changed (see bug #8858).
302 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
303 // Default to the first offset in $wgImageLimits
307 $max = $wgImageLimits[$sizeSel];
309 $maxHeight = $max[1];
311 if ( $this->displayImg
->exists() ) {
313 $page = $request->getIntOrNull( 'page' );
314 if ( is_null( $page ) ) {
318 $params = array( 'page' => $page );
320 $width_orig = $this->displayImg
->getWidth( $page );
321 $width = $width_orig;
322 $height_orig = $this->displayImg
->getHeight( $page );
323 $height = $height_orig;
325 $longDesc = wfMsg( 'parentheses', $this->displayImg
->getLongDesc() );
327 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$out ) );
329 if ( $this->displayImg
->allowInlineDisplay() ) {
332 # "Download high res version" link below the image
333 # $msgsize = wfMsgHtml( 'file-info-size', $width_orig, $height_orig, Linker::formatSize( $this->displayImg->getSize() ), $mime );
334 # We'll show a thumbnail of this image
335 if ( $width > $maxWidth ||
$height > $maxHeight ) {
336 # Calculate the thumbnail size.
337 # First case, the limiting factor is the width, not the height.
338 if ( $width / $height >= $maxWidth / $maxHeight ) {
339 $height = round( $height * $maxWidth / $width );
341 # Note that $height <= $maxHeight now.
343 $newwidth = floor( $width * $maxHeight / $height );
344 $height = round( $height * $newwidth / $width );
346 # Note that $height <= $maxHeight now, but might not be identical
347 # because of rounding.
349 $msgbig = wfMsgHtml( 'show-big-image' );
350 $otherSizes = array();
351 foreach ( $wgImageLimits as $size ) {
352 if ( $size[0] < $width_orig && $size[1] < $height_orig &&
353 $size[0] != $width && $size[1] != $height ) {
354 $otherSizes[] = $this->makeSizeLink( $params, $size[0], $size[1] );
357 $msgsmall = wfMessage( 'show-big-image-preview' )->
358 rawParams( $this->makeSizeLink( $params, $width, $height ) )->
360 if ( count( $otherSizes ) && $this->displayImg
->getRepo()->canTransformVia404() ) {
362 Html
::rawElement( 'span', array( 'class' => 'mw-filepage-other-resolutions' ),
363 wfMessage( 'show-big-image-other' )->rawParams( $lang->pipeList( $otherSizes ) )->
364 params( count( $otherSizes ) )->parse()
367 } elseif ( $width == 0 && $height == 0 ){
368 # Some sort of audio file that doesn't have dimensions
369 # Don't output a no hi res message for such a file
372 # Image is small enough to show full size on image page
373 $msgsmall = wfMessage( 'file-nohires' )->parse();
376 $params['width'] = $width;
377 $params['height'] = $height;
378 $thumbnail = $this->displayImg
->transform( $params );
381 $anchorclose = Html
::rawElement( 'div', array( 'class' => 'mw-filepage-resolutioninfo' ), $msgsmall );
383 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
385 $out->addHTML( '<table class="multipageimage"><tr><td>' );
390 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
393 $out->addHTML( '<div class="fullImageLink" id="file">' .
394 $thumbnail->toHtml( $options ) .
395 $anchorclose . "</div>\n" );
399 $count = $this->displayImg
->pageCount();
402 $label = $out->parse( wfMsg( 'imgmultipageprev' ), false );
403 $link = Linker
::link(
407 array( 'page' => $page - 1 ),
408 array( 'known', 'noclasses' )
410 $thumb1 = Linker
::makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
411 array( 'page' => $page - 1 ) );
416 if ( $page < $count ) {
417 $label = wfMsg( 'imgmultipagenext' );
418 $link = Linker
::link(
422 array( 'page' => $page +
1 ),
423 array( 'known', 'noclasses' )
425 $thumb2 = Linker
::makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
426 array( 'page' => $page +
1 ) );
434 'name' => 'pageselector',
435 'action' => $wgScript,
436 'onchange' => 'document.pageselector.submit();',
439 for ( $i = 1; $i <= $count; $i++
) {
440 $options[] = Xml
::option( $lang->formatNum( $i ), $i, $i == $page );
442 $select = Xml
::tags( 'select',
443 array( 'id' => 'pageselector', 'name' => 'page' ),
444 implode( "\n", $options ) );
447 '</td><td><div class="multipageimagenavbox">' .
448 Xml
::openElement( 'form', $formParams ) .
449 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
450 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
451 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
452 Xml
::closeElement( 'form' ) .
453 "<hr />$thumb1\n$thumb2<br style=\"clear: both\" /></div></td></tr></table>"
457 # if direct link is allowed but it's not a renderable image, show an icon.
458 if ( $this->displayImg
->isSafeFile() ) {
459 $icon = $this->displayImg
->iconThumb();
461 $out->addHTML( '<div class="fullImageLink" id="file">' .
462 $icon->toHtml( array( 'file-link' => true ) ) .
470 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
471 $linktext = $filename;
472 if ( isset( $msgbig ) ) {
473 $linktext = wfEscapeWikiText( $msgbig );
475 $medialink = "[[Media:$filename|$linktext]]";
477 if ( !$this->displayImg
->isSafeFile() ) {
478 $warning = wfMsgNoTrans( 'mediawarning' );
479 // dirmark is needed here to separate the file name, which
480 // most likely ends in Latin characters, from the description,
481 // which may begin with the file type. In RTL environment
482 // this will get messy.
483 // The dirmark, however, must not be immediately adjacent
484 // to the filename, because it can get copied with it.
486 $out->addWikiText( <<<EOT
487 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span> $dirmark<span class="fileInfo">$longDesc</span></div>
488 <div class="mediaWarning">$warning</div>
492 $out->addWikiText( <<<EOT
493 <div class="fullMedia">{$medialink} {$dirmark}<span class="fileInfo">$longDesc</span>
500 if ( !$this->displayImg
->isLocal() ) {
501 $this->printSharedImageText();
504 # Image does not exist
505 if ( !$this->getID() ) {
506 # No article exists either
507 # Show deletion log to be consistent with normal articles
508 LogEventsList
::showLogExtract(
510 array( 'delete', 'move' ),
511 $this->getTitle()->getPrefixedText(),
514 'conds' => array( "log_action != 'revision'" ),
515 'showIfEmpty' => false,
516 'msgKey' => array( 'moveddeleted-notice' )
521 if ( $wgEnableUploads && $user->isAllowed( 'upload' ) ) {
522 // Only show an upload link if the user can upload
523 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
525 'filepage-nofile-link',
526 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->mPage
->getFile()->getName() ) )
529 $nofile = 'filepage-nofile';
531 // Note, if there is an image description page, but
532 // no image, then this setRobotPolicy is overriden
533 // by Article::View().
534 $out->setRobotPolicy( 'noindex,nofollow' );
535 $out->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
536 if ( !$this->getID() && $wgSend404Code ) {
537 // If there is no image, no shared image, and no description page,
538 // output a 404, to be consistent with articles.
539 $request->response()->header( 'HTTP/1.1 404 Not Found' );
542 $out->setFileVersion( $this->displayImg
);
546 * Creates an thumbnail of specified size and returns an HTML link to it
547 * @param $params array Scaler parameters
552 private function makeSizeLink( $params, $width, $height ) {
553 $params['width'] = $width;
554 $params['height'] = $height;
555 $thumbnail = $this->displayImg
->transform( $params );
556 if ( $thumbnail && !$thumbnail->isError() ) {
557 return Html
::rawElement( 'a', array(
558 'href' => $thumbnail->getUrl(),
559 'class' => 'mw-thumbnail-link'
560 ), wfMessage( 'show-big-image-size' )->numParams(
561 $thumbnail->getWidth(), $thumbnail->getHeight()
569 * Show a notice that the file is from a shared repository
571 protected function printSharedImageText() {
572 $out = $this->getContext()->getOutput();
575 $descUrl = $this->mPage
->getFile()->getDescriptionUrl();
576 $descText = $this->mPage
->getFile()->getDescriptionText();
578 /* Add canonical to head if there is no local page for this shared file */
579 if( $descUrl && $this->mPage
->getID() == 0 ) {
580 $out->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
583 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
584 $repo = $this->mPage
->getFile()->getRepo()->getDisplayName();
586 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
587 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
588 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
589 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
591 $out->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
595 $this->mExtraDescription
= $descText;
599 public function getUploadUrl() {
601 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
602 return $uploadTitle->getFullURL( array(
603 'wpDestFile' => $this->mPage
->getFile()->getName(),
609 * Print out the various links at the bottom of the image page, e.g. reupload,
610 * external editing (and instructions link) etc.
612 protected function uploadLinksBox() {
613 global $wgEnableUploads, $wgUseExternalEditor;
615 if ( !$wgEnableUploads ) {
620 if ( !$this->mPage
->getFile()->isLocal() ) {
624 $out = $this->getContext()->getOutput();
625 $out->addHTML( "<br /><ul>\n" );
627 # "Upload a new version of this file" link
628 if ( UploadBase
::userCanReUpload( $this->getContext()->getUser(), $this->mPage
->getFile()->name
) ) {
629 $ulink = Linker
::makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
630 $out->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
633 # External editing link
634 if ( $wgUseExternalEditor ) {
635 $elink = Linker
::link(
637 wfMsgHtml( 'edit-externally' ),
641 'externaledit' => 'true',
644 array( 'known', 'noclasses' )
647 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
648 wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) .
653 $out->addHTML( "</ul>\n" );
656 protected function closeShowImage() { } # For overloading
659 * If the page we've just displayed is in the "Image" namespace,
660 * we follow it with an upload history of the image and its usage.
662 protected function imageHistory() {
664 $out = $this->getContext()->getOutput();
665 $pager = new ImageHistoryPseudoPager( $this );
666 $out->addHTML( $pager->getBody() );
667 $out->preventClickjacking( $pager->getPreventClickjacking() );
669 $this->mPage
->getFile()->resetHistory(); // free db resources
671 # Exist check because we don't want to show this on pages where an image
672 # doesn't exist along with the noimage message, that would suck. -ævar
673 if ( $this->mPage
->getFile()->exists() ) {
674 $this->uploadLinksBox();
681 * @return ResultWrapper
683 protected function queryImageLinks( $target, $limit ) {
684 $dbr = wfGetDB( DB_SLAVE
);
687 array( 'imagelinks', 'page' ),
688 array( 'page_namespace', 'page_title', 'page_is_redirect', 'il_to' ),
689 array( 'il_to' => $target, 'il_from = page_id' ),
691 array( 'LIMIT' => $limit +
1, 'ORDER BY' => 'il_from', )
695 protected function imageLinks() {
698 $out = $this->getContext()->getOutput();
699 $res = $this->queryImageLinks( $this->getTitle()->getDbKey(), $limit +
1);
701 $redirects = array();
702 foreach ( $res as $row ) {
703 if ( $row->page_is_redirect
) {
704 $redirects[$row->page_title
] = array();
708 $count = count( $rows );
710 $hasMore = $count > $limit;
711 if ( !$hasMore && count( $redirects ) ) {
712 $res = $this->queryImageLinks( array_keys( $redirects ),
713 $limit - count( $rows ) +
1 );
714 foreach ( $res as $row ) {
715 $redirects[$row->il_to
][] = $row;
718 $hasMore = ( $res->numRows() +
count( $rows ) ) > $limit;
723 Html
::rawElement( 'div',
724 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
730 $out->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
732 $out->addWikiMsg( 'linkstoimage', $count );
734 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
735 $out->addWikiMsg( 'linkstoimage-more',
736 $this->getContext()->getLanguage()->formatNum( $limit ),
737 $this->getTitle()->getPrefixedDBkey()
742 Html
::openElement( 'ul',
743 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
747 // Sort the list by namespace:title
748 usort( $rows, array( $this, 'compare' ) );
750 // Create links for every element
752 foreach( $rows as $element ) {
754 if ( $currentCount > $limit ) {
758 $link = Linker
::linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
759 if ( !isset( $redirects[$element->page_title
] ) ) {
762 $ul = "<ul class='mw-imagepage-redirectstofile'>\n";
763 foreach ( $redirects[$element->page_title
] as $row ) {
765 if ( $currentCount > $limit ) {
769 $link2 = Linker
::linkKnown( Title
::makeTitle( $row->page_namespace
, $row->page_title
) );
770 $ul .= Html
::rawElement(
772 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
777 $liContents = wfMessage( 'linkstoimage-redirect' )->rawParams(
778 $link, $ul )->parse();
780 $out->addHTML( Html
::rawElement(
782 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
788 $out->addHTML( Html
::closeElement( 'ul' ) . "\n" );
791 // Add a links to [[Special:Whatlinkshere]]
792 if ( $count > $limit ) {
793 $out->addWikiMsg( 'morelinkstoimage', $this->getTitle()->getPrefixedDBkey() );
795 $out->addHTML( Html
::closeElement( 'div' ) . "\n" );
798 protected function imageDupes() {
800 $out = $this->getContext()->getOutput();
802 $dupes = $this->mPage
->getDuplicates();
803 if ( count( $dupes ) == 0 ) {
807 $out->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
808 $out->addWikiMsg( 'duplicatesoffile',
809 $this->getContext()->getLanguage()->formatNum( count( $dupes ) ), $this->getTitle()->getDBkey()
811 $out->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
816 foreach ( $dupes as $file ) {
818 if ( $file->isLocal() ) {
819 $link = Linker
::link(
824 array( 'known', 'noclasses' )
827 $link = Linker
::makeExternalLink( $file->getDescriptionUrl(),
828 $file->getTitle()->getPrefixedText() );
829 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
831 $out->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
833 $out->addHTML( "</ul></div>\n" );
837 * Delete the file, or an earlier version of it
839 public function delete() {
840 $file = $this->mPage
->getFile();
841 if ( !$file->exists() ||
!$file->isLocal() ||
$file->getRedirected() ) {
842 // Standard article deletion
847 $deleter = new FileDeleteForm( $file );
852 * Display an error with a wikitext description
854 * @param $description String
856 function showError( $description ) {
857 $out = $this->getContext()->getOutput();
858 $out->setPageTitle( wfMessage( 'internalerror' ) );
859 $out->setRobotPolicy( 'noindex,nofollow' );
860 $out->setArticleRelated( false );
861 $out->enableClientCache( false );
862 $out->addWikiText( $description );
866 * Callback for usort() to do link sorts by (namespace, title)
867 * Function copied from Title::compare()
869 * @param $a object page to compare with
870 * @param $b object page to compare with
871 * @return Integer: result of string comparison, or namespace comparison
873 protected function compare( $a, $b ) {
874 if ( $a->page_namespace
== $b->page_namespace
) {
875 return strcmp( $a->page_title
, $b->page_title
);
877 return $a->page_namespace
- $b->page_namespace
;
883 * Builds the image revision log shown on image pages
887 class ImageHistoryList
extends ContextSource
{
902 protected $imagePage;
909 protected $repo, $showThumb;
910 protected $preventClickjacking = false;
913 * @param ImagePage $imagePage
915 public function __construct( $imagePage ) {
916 global $wgShowArchiveThumbnails;
917 $this->current
= $imagePage->getFile();
918 $this->img
= $imagePage->getDisplayedFile();
919 $this->title
= $imagePage->getTitle();
920 $this->imagePage
= $imagePage;
921 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
922 $this->setContext( $imagePage->getContext() );
928 public function getImagePage() {
929 return $this->imagePage
;
935 public function getFile() {
940 * @param $navLinks string
943 public function beginImageHistoryList( $navLinks = '' ) {
944 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
945 . "<div id=\"mw-imagepage-section-filehistory\">\n"
946 . $this->getOutput()->parse( wfMsgNoTrans( 'filehist-help' ) )
948 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
950 . ( $this->current
->isLocal() && ( $this->getUser()->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
951 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
952 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
953 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
954 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
955 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
960 * @param $navLinks string
963 public function endImageHistoryList( $navLinks = '' ) {
964 return "</table>\n$navLinks\n</div>\n";
972 public function imageHistoryLine( $iscur, $file ) {
975 $user = $this->getUser();
976 $lang = $this->getLanguage();
977 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
978 $img = $iscur ?
$file->getName() : $file->getArchiveName();
979 $userId = $file->getUser( 'id' );
980 $userText = $file->getUser( 'text' );
981 $description = $file->getDescription();
983 $local = $this->current
->isLocal();
984 $row = $selected = '';
987 if ( $local && ( $user->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
989 # Link to remove from history
990 if ( $user->isAllowed( 'delete' ) ) {
991 $q = array( 'action' => 'delete' );
993 $q['oldimage'] = $img;
995 $row .= Linker
::link(
997 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
998 array(), $q, array( 'known' )
1001 # Link to hide content. Don't show useless link to people who cannot hide revisions.
1002 $canHide = $user->isAllowed( 'deleterevision' );
1003 if ( $canHide ||
( $user->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
1004 if ( $user->isAllowed( 'delete' ) ) {
1007 // If file is top revision or locked from this user, don't link
1008 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
1009 $del = Linker
::revDeleteLinkDisabled( $canHide );
1011 list( $ts, ) = explode( '!', $img, 2 );
1013 'type' => 'oldimage',
1014 'target' => $this->title
->getPrefixedText(),
1017 $del = Linker
::revDeleteLink( $query,
1018 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1025 // Reversion link/current indicator
1028 $row .= wfMsgHtml( 'filehist-current' );
1029 } elseif ( $local && $user->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
1030 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1031 $row .= wfMsgHtml( 'filehist-revert' );
1033 $row .= Linker
::link(
1035 wfMsgHtml( 'filehist-revert' ),
1038 'action' => 'revert',
1040 'wpEditToken' => $user->getEditToken( $img )
1042 array( 'known', 'noclasses' )
1048 // Date/time and image link
1049 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
1050 $selected = "class='filehistory-selected'";
1052 $row .= "<td $selected style='white-space: nowrap;'>";
1053 if ( !$file->userCan( File
::DELETED_FILE
) ) {
1054 # Don't link to unviewable files
1055 $row .= '<span class="history-deleted">' . $lang->timeanddate( $timestamp, true ) . '</span>';
1056 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1058 $this->preventClickjacking();
1059 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1060 # Make a link to review the image
1061 $url = Linker
::link(
1063 $lang->timeanddate( $timestamp, true ),
1066 'target' => $this->title
->getPrefixedText(),
1068 'token' => $user->getEditToken( $img )
1070 array( 'known', 'noclasses' )
1073 $url = $lang->timeanddate( $timestamp, true );
1075 $row .= '<span class="history-deleted">' . $url . '</span>';
1077 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1078 $row .= Xml
::element( 'a', array( 'href' => $url ), $lang->timeanddate( $timestamp, true ) );
1083 if ( $this->showThumb
) {
1084 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1087 // Image dimensions + size
1089 $row .= htmlspecialchars( $file->getDimensionsString() );
1090 $row .= ' <span style="white-space: nowrap;">(' . Linker
::formatSize( $file->getSize() ) . ')</span>';
1095 // Hide deleted usernames
1096 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1097 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1100 $row .= Linker
::userLink( $userId, $userText ) . ' <span style="white-space: nowrap;">' .
1101 Linker
::userToolLinks( $userId, $userText ) . '</span>';
1103 $row .= htmlspecialchars( $userText );
1108 // Don't show deleted descriptions
1109 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1110 $row .= '<td><span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></td>';
1112 $row .= '<td dir="' . $wgContLang->getDir() . '">' . Linker
::formatComment( $description, $this->title
) . '</td>';
1116 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1117 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1119 return "<tr{$classAttr}>{$row}</tr>\n";
1126 protected function getThumbForLine( $file ) {
1127 $lang = $this->getLanguage();
1128 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1133 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1135 $thumbnail = $file->transform( $params );
1137 'alt' => wfMsg( 'filehist-thumbtext',
1138 $lang->timeanddate( $timestamp, true ),
1139 $lang->date( $timestamp, true ),
1140 $lang->time( $timestamp, true ) ),
1141 'file-link' => true,
1144 if ( !$thumbnail ) {
1145 return wfMsgHtml( 'filehist-nothumb' );
1148 return $thumbnail->toHtml( $options );
1150 return wfMsgHtml( 'filehist-nothumb' );
1155 * @param $enable bool
1157 protected function preventClickjacking( $enable = true ) {
1158 $this->preventClickjacking
= $enable;
1164 public function getPreventClickjacking() {
1165 return $this->preventClickjacking
;
1169 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1170 protected $preventClickjacking = false;
1183 * @param ImagePage $imagePage
1185 function __construct( $imagePage ) {
1186 parent
::__construct();
1187 $this->mImagePage
= $imagePage;
1188 $this->mTitle
= clone ( $imagePage->getTitle() );
1189 $this->mTitle
->setFragment( '#filehistory' );
1191 $this->mHist
= array();
1192 $this->mRange
= array( 0, 0 ); // display range
1198 function getTitle() {
1199 return $this->mTitle
;
1202 function getQueryInfo() {
1209 function getIndexField() {
1214 * @param $row object
1217 function formatRow( $row ) {
1224 function getBody() {
1227 if ( count( $this->mHist
) ) {
1228 $list = new ImageHistoryList( $this->mImagePage
);
1229 # Generate prev/next links
1230 $navLink = $this->getNavigationBar();
1231 $s = $list->beginImageHistoryList( $navLink );
1232 // Skip rows there just for paging links
1233 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1234 $file = $this->mHist
[$i];
1235 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1237 $s .= $list->endImageHistoryList( $navLink );
1239 if ( $list->getPreventClickjacking() ) {
1240 $this->preventClickjacking();
1246 function doQuery() {
1247 if ( $this->mQueryDone
) {
1250 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1251 if ( !$this->mImg
->exists() ) {
1254 $queryLimit = $this->mLimit +
1; // limit plus extra row
1255 if ( $this->mIsBackwards
) {
1256 // Fetch the file history
1257 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1258 // The current rev may not meet the offset/limit
1259 $numRows = count( $this->mHist
);
1260 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1261 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1264 // The current rev may not meet the offset
1265 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1266 $this->mHist
[] = $this->mImg
;
1268 // Old image versions (fetch extra row for nav links)
1269 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1270 // Fetch the file history
1271 $this->mHist
= array_merge( $this->mHist
,
1272 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1274 $numRows = count( $this->mHist
); // Total number of query results
1276 # Index value of top item in the list
1277 $firstIndex = $this->mIsBackwards ?
1278 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1279 # Discard the extra result row if there is one
1280 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1281 if ( $this->mIsBackwards
) {
1282 # Index value of item past the index
1283 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1284 # Index value of bottom item in the list
1285 $lastIndex = $this->mHist
[1]->getTimestamp();
1287 $this->mRange
= array( 1, $numRows - 1 );
1289 # Index value of item past the index
1290 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1291 # Index value of bottom item in the list
1292 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1294 $this->mRange
= array( 0, $numRows - 2 );
1297 # Setting indexes to an empty string means that they will be
1298 # omitted if they would otherwise appear in URLs. It just so
1299 # happens that this is the right thing to do in the standard
1300 # UI, in all the relevant cases.
1301 $this->mPastTheEndIndex
= '';
1302 # Index value of bottom item in the list
1303 $lastIndex = $this->mIsBackwards ?
1304 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1306 $this->mRange
= array( 0, $numRows - 1 );
1311 $this->mPastTheEndIndex
= '';
1313 if ( $this->mIsBackwards
) {
1314 $this->mIsFirst
= ( $numRows < $queryLimit );
1315 $this->mIsLast
= ( $this->mOffset
== '' );
1316 $this->mLastShown
= $firstIndex;
1317 $this->mFirstShown
= $lastIndex;
1319 $this->mIsFirst
= ( $this->mOffset
== '' );
1320 $this->mIsLast
= ( $numRows < $queryLimit );
1321 $this->mLastShown
= $lastIndex;
1322 $this->mFirstShown
= $firstIndex;
1324 $this->mQueryDone
= true;
1328 * @param $enable bool
1330 protected function preventClickjacking( $enable = true ) {
1331 $this->preventClickjacking
= $enable;
1337 public function getPreventClickjacking() {
1338 return $this->preventClickjacking
;